<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01_函数柯里化在传参时的体现</title>
</head>

<body>
    <div id="app"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class MyForm extends React.Component{
            state = {
                userName:"",
                passWord:""
            }

            // 这里的函数返回一个函数去处理剩下的参数，我们称为函数柯里化
            saveFormData = (key)=>{
                // 返回一个函数作为onChange的回调
                return (e)=>{
                    console.log(key,e);
                    this.setState({
                        [key]:e.target.value
                    })
                }
            }

            submitForm = (event)=>{
                // 阻止表单提交默认事件
                event.preventDefault()
                // 从this.state中解构获取表单数据
                const {userName,passWord} = this.state
                // 显示数据
                alert(`用户名是：${userName},密码是：${passWord}`)
                
            }
            render(){
                return (
                    <form onSubmit={this.submitForm}>
                        用户名：<input type="text" name="username" onChange={this.saveFormData('userName')}/><br/>
                        密码：<input type="password" name="password" onChange={this.saveFormData('passWord')}/><br/>
                        <button>登录</button>
                    </form>
                )
            }
        }

        ReactDOM.render(<MyForm/>,document.getElementById("app"))
    </script>
</body>

</html>